package com.example.yuhuaqiu.util;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by yuhuaqiu on 16/5/14.
 */
public class MyViewPager extends ViewPager {

    private float beforeX;
    private boolean isCanScroll = false;

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyViewPager(Context context) {
        super(context);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (isCanScroll){
            return super.dispatchTouchEvent(ev);
        }else {
            switch (ev.getAction()){
                case MotionEvent.ACTION_DOWN:
                    beforeX = ev.getX();
                    break;
                case MotionEvent.ACTION_MOVE:
                    float motionValue = ev.getX()-beforeX;
                    if (motionValue<0){
                        return true;
                    }
                    beforeX = ev.getX();
                    break;
                default:
                    break;
            }
            return super.dispatchTouchEvent(ev);
        }
    }

    public boolean isScrollble(){
        return isCanScroll;
    }

    public void setScrollble(boolean isCanScroll){
        this.isCanScroll = isCanScroll;
    }
}
